/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Control.Controlador;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import java.util.Vector;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Administrador
 */
public class Fight_Frame extends JFrame implements Observer, ActionListener {

    public Controlador gestorPrincipal;
    private JPanel jpVs;
    private JPanel jpLeft;
    private JPanel jpPlayerText;
    private JPanel jpPlayer;
    private JPanel jpEnemyText;
    private JPanel jpEnemy;
    private JPanel jpRight;
    private JPanel jpPlayerForm;
    private JPanel jpSur;
    private JPanel jp10;
    private JPanel jp11;
    private JPanel jp12;
    private JPanel jp13;
    private JPanel jp14;
    private JPanel jpEnemyForm;
    private JPanel jp16;
    private JPanel jp17;
    private JPanel jpResults;
    private JPanel jp19;
    private JPanel jp20;
    private JPanel jpTitulo;
    private JButton jbAD;
    private JLabel jlPlayer;
    private JLabel jlEnemy;
    private JLabel jlPlayerLife;
    private JLabel jlEnemyLife;
    private JLabel jlChoosePower;
    private JLabel jlVs;
    private JLabel jlPlayerImage;
    private JLabel jlEnemyImage;
    private JLabel jlRound;
    private JLabel jlHit;
    private JLabel jlNumber;
    private ImageIcon iiVs;
    private ImageIcon iiPlayer;
    private ImageIcon iiEnemy;
    private JProgressBar jpbPlayer;
    private JProgressBar jpbEnemy;
    private JComboBox jcbPowers;
    private JTextArea jtaResults;
    private JScrollPane jspResults;
    private Vector v_powers;
    private boolean bandera;
    private DefaultComboBoxModel model;
    private Random randomGenerator;

    public Fight_Frame(Controlador controlador) {
        gestorPrincipal = controlador;
        gestorPrincipal.registrarObservador(this);

        bandera = true;
        randomGenerator = new Random();
        jpVs = new JPanel();
        jpLeft = new JPanel();
        jpPlayerText = new JPanel();
        jpPlayer = new JPanel();
        jpEnemyText = new JPanel();
        jpEnemy = new JPanel();
        jpRight = new JPanel();
        jpPlayerForm = new JPanel();
        jpSur = new JPanel();
        jp10 = new JPanel();
        jp11 = new JPanel();
        jp12 = new JPanel();
        jp13 = new JPanel();
        jp14 = new JPanel();
        jpEnemyForm = new JPanel();
        jp16 = new JPanel();
        jp17 = new JPanel();
        jpResults = new JPanel();
        jp19 = new JPanel();
        jp20 = new JPanel();
        jpTitulo = new JPanel();

        jtaResults = new JTextArea(20, 30);
        jspResults = new JScrollPane(jtaResults);

        jpVs.setBackground(Color.GRAY.darker());
        jpLeft.setBackground(Color.GRAY.darker());
        jpPlayerText.setBackground(Color.GRAY.darker());
        jpPlayer.setBackground(Color.GRAY.darker());
        jpEnemyText.setBackground(Color.GRAY.darker());
        jpEnemy.setBackground(Color.GRAY.darker());
        jpRight.setBackground(Color.GRAY.darker());
        jpPlayerForm.setBackground(Color.GRAY.darker());
        jpSur.setBackground(Color.GRAY.darker());
        jp10.setBackground(Color.GRAY.darker());
        jp11.setBackground(Color.GRAY.darker());
        jp12.setBackground(Color.GRAY.darker());
        jp13.setBackground(Color.GRAY.darker());
        jp14.setBackground(Color.GRAY.darker());
        jpEnemyForm.setBackground(Color.GRAY.darker());
        jp16.setBackground(Color.GRAY.darker());
        jp17.setBackground(Color.GRAY.darker());
        jpResults.setBackground(Color.GRAY.darker());
        jp19.setBackground(Color.GRAY.darker());
        jp20.setBackground(Color.GRAY.darker());
        jpTitulo.setBackground(Color.GRAY.darker());

        iiVs = new ImageIcon(((new ImageIcon(getClass().getResource("/img/vs.png")))
                .getImage()).getScaledInstance(150, 150, Image.SCALE_SMOOTH));
        iiEnemy = new ImageIcon(((new ImageIcon(getClass().getResource(gestorPrincipal.jugador2.getUrl())))
                .getImage()).getScaledInstance(320, 550, Image.SCALE_SMOOTH));
        iiPlayer = new ImageIcon(((new ImageIcon(getClass().getResource(gestorPrincipal.jugador1.getUrl())))
                .getImage()).getScaledInstance(320, 550, Image.SCALE_SMOOTH));


        jbAD = new JButton("ATACAR!!!");

        jlPlayer = new JLabel("Mi jugador");
        jlEnemy = new JLabel("Enemigo");
        jlHit = new JLabel("Hit: ");
        jlHit.setFont(new Font("Magneto", Font.BOLD, 20));
        jlHit.setForeground(Color.YELLOW.darker());
        jlNumber = new JLabel();
        jlNumber.setFont(new Font("Magneto", Font.BOLD, 20));
        jlPlayerLife = new JLabel("Vida:");
        jlPlayerLife.setFont(new Font("Magneto", Font.BOLD, 20));
        jlPlayerLife.setForeground(Color.YELLOW.darker());
        jlEnemyLife = new JLabel("Vida:");
        jlEnemyLife.setFont(new Font("Magneto", Font.BOLD, 20));
        jlEnemyLife.setForeground(Color.YELLOW.darker());
        jlChoosePower = new JLabel("Escoger ataque:");
        jlChoosePower.setFont(new Font("Magneto", Font.BOLD, 20));
        jlChoosePower.setForeground(Color.YELLOW.darker());
        jlVs = new JLabel(iiVs);
        jlPlayerImage = new JLabel(iiPlayer);
        jlEnemyImage = new JLabel(iiEnemy);
        jlPlayer.setText("Mi jugador es: " + gestorPrincipal.jugador1.getNombre());
        jlPlayer.setFont(new Font("Impact", Font.BOLD, 20));
        jlPlayer.setForeground(Color.GREEN.darker());
        jlEnemy.setText("Enemigo: " + gestorPrincipal.jugador2.getNombre());
        jlEnemy.setFont(new Font("Impact", Font.BOLD, 20));
        jlEnemy.setForeground(Color.red.darker());
        jlRound = new JLabel();
        jlRound.setText("Round: " + gestorPrincipal.Numpelea());
        jlRound.setFont(new Font("Showcard Gothic", Font.BOLD, 28));
        jlRound.setForeground(Color.YELLOW.darker());

        jpbPlayer = new JProgressBar(0, gestorPrincipal.jugador1.getVida());
        jpbEnemy = new JProgressBar(0, gestorPrincipal.jugador2.getVida());

        v_powers = new Vector();
        for (int i = 0; i < 3; i++) {
            v_powers.add(gestorPrincipal.jugador1.getAtaques().get(i).toString());
        }
        model = new DefaultComboBoxModel(v_powers);
        jcbPowers = new JComboBox(model);

        this.jbAD.addActionListener(this);

    }

    public void create() {
        this.jtaResults.setEditable(false);
        this.jpbEnemy.setBorderPainted(true);
        this.jpbEnemy.setStringPainted(true);
        this.jpbEnemy.setString(String.valueOf(gestorPrincipal.jugador2.getVida()));
        this.jpbEnemy.setForeground(Color.GREEN.darker());
        this.jpbEnemy.setValue(1000);

        this.jpbPlayer.setBorderPainted(true);
        this.jpbPlayer.setStringPainted(true);
        this.jpbPlayer.setString(String.valueOf(gestorPrincipal.jugador1.getVida()));
        this.jpbPlayer.setForeground(Color.GREEN.darker());
        this.jpbPlayer.setValue(1000);

        this.jpVs.setLayout(new BorderLayout());
        this.jpVs.add(jlVs, BorderLayout.NORTH);
        this.jpResults.add(this.jspResults);
        this.jpVs.add(this.jpResults, BorderLayout.CENTER);


        this.jpPlayerText.add(this.jlPlayer);
        this.jpPlayer.add(this.jlPlayerImage);
        this.jpLeft.setLayout(new BorderLayout());
        this.jpLeft.add(this.jpPlayerText, BorderLayout.NORTH);
        this.jpLeft.add(this.jpPlayer, BorderLayout.CENTER);

        this.jpEnemyText.add(this.jlEnemy);
        this.jpEnemy.add(this.jlEnemyImage);
        this.jpRight.setLayout(new BorderLayout());
        this.jpRight.add(this.jpEnemyText, BorderLayout.NORTH);
        this.jpRight.add(this.jpEnemy, BorderLayout.CENTER);

        this.jpPlayerForm.setLayout(new GridLayout(2, 2));
        this.jp10.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.jp11.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.jp12.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.jp13.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.jp10.add(this.jlPlayerLife);
        this.jp11.add(this.jlChoosePower);
        this.jp12.add(this.jpbPlayer);
        this.jp13.add(this.jcbPowers);
        this.jpPlayerForm.add(this.jp10);
        this.jpPlayerForm.add(this.jp12);
        this.jpPlayerForm.add(this.jp11);
        this.jpPlayerForm.add(this.jp13);

        this.jp14.setLayout(new GridLayout(2, 1));
        this.jp20.add(this.jlHit);
        this.jp20.add(this.jlNumber);
        this.jp19.add(this.jbAD);
        this.jp14.add(jp20);
        this.jp14.add(jp19);



        this.jp16.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.jp17.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.jp16.add(this.jlEnemyLife);
        this.jp17.add(this.jpbEnemy);
        this.jpEnemyForm.add(this.jp16);
        this.jpEnemyForm.add(this.jp17);




        this.jpSur.setLayout(new GridLayout(1, 3));
        this.jpSur.add(this.jpPlayerForm);
        this.jpSur.add(this.jp14);
        this.jpSur.add(this.jpEnemyForm);

        this.jpTitulo.add(this.jlRound);

        this.setLayout(new BorderLayout());
        this.add(this.jpTitulo, BorderLayout.NORTH);
        this.add(this.jpLeft, BorderLayout.WEST);
        this.add(this.jpRight, BorderLayout.EAST);
        this.add(this.jpVs, BorderLayout.CENTER);
        this.add(this.jpSur, BorderLayout.SOUTH);

        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    @Override
    public void update(Observable o, Object arg) {
        updateInfo((String) arg);
    }

    public void nextbattle() {
    }

    public void updateInfo(String msj) {
        this.jtaResults.append("\n" + msj);

        this.jpbPlayer.setValue(gestorPrincipal.jugador1.getVida());
        this.jpbPlayer.setString(String.valueOf(gestorPrincipal.jugador1.getVida()));

        this.jpbEnemy.setValue(gestorPrincipal.jugador2.getVida());
        this.jpbEnemy.setString(String.valueOf(gestorPrincipal.jugador2.getVida()));

    }

    public boolean revivir() {
        if (gestorPrincipal.random(100, 1) <= 50) {
            gestorPrincipal.jugador1.setVida(500);
            JOptionPane.showMessageDialog(this, "Los caballeros de bronce han sido \n revividos por Atena");
            return true;
        }
        return false;
    }

    public void termino() {
        if (gestorPrincipal.escenarios > 0) {
            if (gestorPrincipal.jugador1.getVida() <= 0) {
                if (gestorPrincipal.jugador1.getNombre().equals("Caballeros de bronce")) {
                    if (revivir()) {
                        return;
                    }
                }
                JOptionPane.showMessageDialog(this, "PERDIÓ... JAJAJA!!!");
                this.dispose();
                gestorPrincipal =
                        new Controlador();
                Main_Frame principal =
                        new Main_Frame(gestorPrincipal);

                principal.init();

            } else if (gestorPrincipal.jugador2.getVida() <= 0) {
                if (gestorPrincipal.jugador1.getNombre().equals("Caballeros de bronce")) {
                    if (revivir()) {
                        return;
                    }
                }
                //gestorPrincipal.escenarios -= 1;
                JOptionPane.showMessageDialog(this, "GANASTE ESTE DUELO... FELICIDADES!!!");
                this.dispose();
                gestorPrincipal.establishNextScenary();
                //this.dispose();

            }
        }
    }

    public int random(int maxRange, int minRange) {
        return (randomGenerator.nextInt(maxRange - minRange) + minRange);
    }

    public void sonidoAtaque() {
        try {
            try {
                gestorPrincipal.sonido.close();
                gestorPrincipal.sonido.open(AudioSystem.getAudioInputStream(getClass().getResource("/Aplicacion/golpe.wav")));
                gestorPrincipal.sonido.start();
            } catch (LineUnavailableException ex) {
                //Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (UnsupportedAudioFileException | IOException ex) {
            //Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (this.jbAD == e.getSource()) {
            if (bandera) {

                gestorPrincipal.jugador1.setAtaqueActual(gestorPrincipal.jugador1.getAtaques().get(this.jcbPowers.getSelectedIndex()));
                gestorPrincipal.jugador2.setDefensaActual(gestorPrincipal.jugador2.getDefensas()
                        .get(random(gestorPrincipal.jugador2.getNumDefensa(), 1)));

                if (gestorPrincipal.puedoAtacar(gestorPrincipal.jugador1)) {
                    this.sonidoAtaque();
                    gestorPrincipal.atacarEnemigo(gestorPrincipal.jugador1, gestorPrincipal.jugador2);

                    if (gestorPrincipal.dano > 0) {
                        this.jlNumber.setText("-" + String.valueOf(Math.abs(gestorPrincipal.dano)));
                        this.jlNumber.setForeground(Color.GREEN.darker());
                    } else {
                        this.jlNumber.setText("+" + String.valueOf(Math.abs(gestorPrincipal.dano)));
                        this.jlNumber.setForeground(Color.red.darker());
                    }
                } else {
                    this.jtaResults.append("\n" + gestorPrincipal.jugador1.getNombre() + " no acertó el ataque\n"
                            + "----------------------");
                    this.jlNumber.setText("0");
                    this.jlNumber.setForeground(Color.white);
                }


                termino();
                if (gestorPrincipal.isHabi()) {
                    gestorPrincipal.setHabi(false);

                } else {
                    this.bandera = false;
                    jlChoosePower.setText("Escoger defensa");
                    this.jbAD.setText("DEFENDER!!!");
                    model.removeAllElements();

                    for (int i = 0; i < gestorPrincipal.jugador1.getDefensas().size(); i++) {
                        model.addElement(gestorPrincipal.jugador1.getDefensas().get(i).toString());
                    }
                    this.jpSur.updateUI();
                }

            } else {
                gestorPrincipal.sonido.close();

//                Random generator = new Random();
//                int random = generator.nextInt(2) + 1;

                gestorPrincipal.jugador2.setAtaqueActual(gestorPrincipal.jugador2.getAtaques()
                        .get(random(gestorPrincipal.jugador2.getNumDefensa(), 1)));
                gestorPrincipal.jugador1.setDefensaActual(gestorPrincipal.jugador1.getDefensas().get(this.jcbPowers.getSelectedIndex()));

                if (gestorPrincipal.puedoAtacar(gestorPrincipal.jugador2)) {
                    gestorPrincipal.atacarEnemigo(gestorPrincipal.jugador2, gestorPrincipal.jugador1);

                    if (gestorPrincipal.dano > 0) {
                        this.jlNumber.setText("-" + String.valueOf(Math.abs(gestorPrincipal.dano)));
                        this.jlNumber.setForeground(Color.red.darker());
                    } else {
                        this.jlNumber.setText("+" + String.valueOf(Math.abs(gestorPrincipal.dano)));
                        this.jlNumber.setForeground(Color.green.darker());
                    }
                } else {
                    this.jtaResults.append("\n" + gestorPrincipal.jugador2.getNombre() + " no acertó el ataque\n"
                            + "----------------------");
                    this.jlNumber.setText("0");
                    this.jlNumber.setForeground(Color.white);
                }

                this.bandera = true;

                termino();

                jlChoosePower.setText("Escoger ataque");
                this.jbAD.setText("ATACAR!!!");
                model.removeAllElements();

                for (int i = 0; i < gestorPrincipal.jugador1.getAtaques().size(); i++) {
                    model.addElement(gestorPrincipal.jugador1.getAtaques().get(i).toString());
                }
                this.jpSur.updateUI();

            }
        }
    }
}
